package zuel.innovation.disley.announcement.controller;

import cn.fabrice.common.constant.BaseConstants;
import cn.fabrice.common.pojo.BaseResult;
import cn.fabrice.common.pojo.DataResult;
import cn.fabrice.jfinal.annotation.Param;
import cn.fabrice.jfinal.annotation.ValidateParam;
import cn.fabrice.jfinal.constant.ValidateRuleConstants;
import cn.fabrice.jfinal.ext.cros.interceptor.CrossInterceptor;
import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.core.Path;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import zuel.innovation.disley.announcement.service.AnnouncementService;
import zuel.innovation.disley.common.module.Announcement;
import zuel.innovation.disley.common.module.User;
import zuel.innovation.disley.interceptor.AuthInterceptor;
import zuel.innovation.disley.user.service.UserService;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@Before(CrossInterceptor.class)
@Path("/announcement")
@ValidateParam
public class AnnouncementController extends Controller {
    @Inject
    AnnouncementService announcementService;
    @Inject
    UserService userService;


    /**
     * 创建外部公告
     *
     * @param anTitle  公告标题
     * @param anText   公告内容
     * @throws ParseException
     */
    @Param(name = "anTitle", required = true)
    @Param(name = "anText", required = true)
    public void createExternalAnnouncement(String anTitle, String anText) throws ParseException {
        Announcement announcement = new Announcement();
        announcement.setAnTitle(anTitle);
        int uId = getAttr(BaseConstants.ACCOUNT_ID);
        Kv cond = Kv.by("uId",uId);
        User user = userService.get(cond,"getUserById");
        String anAuthor = user.getUName();
        announcement.setAnAuthor(anAuthor);
        announcement.setAnText(anText);
        announcement.setAnType(0);//0-外部公告
        SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date an_create = df.parse(df.format(System.currentTimeMillis()));
        announcement.setAnCreate(an_create);
        renderJson(announcement.save() ? DataResult.data(announcement) : BaseResult.fail());
    }

    /**
     * 创建招新公告
     *
     * @param anTitle  公告标题
     * @param anText   公告内容
     * @throws ParseException
     */
    @Param(name = "anTitle", required = true)
    @Param(name = "anText", required = true)
    public void createRecruitAnnouncement(String anTitle,  String anText) throws ParseException {
        Announcement announcement = new Announcement();
        announcement.setAnTitle(anTitle);
        int uId = getAttr(BaseConstants.ACCOUNT_ID);
        Kv cond = Kv.by("uId",uId);
        User user = userService.get(cond,"getUserById");
        String anAuthor = user.getUName();
        announcement.setAnAuthor(anAuthor);
        announcement.setAnText(anText);
        announcement.setAnType(2);//2-招新公告
        SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date an_create = df.parse(df.format(System.currentTimeMillis()));
        announcement.setAnCreate(an_create);
        renderJson(announcement.save() ? DataResult.data(announcement) : BaseResult.fail());
    }

    /**
     * 创建内部公告
     *
     * @param anTitle  公告标题
     * @param anText   公告内容
     * @throws ParseException
     */
    @Param(name = "anTitle", required = true)
    @Param(name = "anText", required = true)
    public void createInternalAnnouncement(String anTitle,  String anText) throws ParseException {
        Announcement announcement = new Announcement();
        announcement.setAnTitle(anTitle);
        int uId = getAttr(BaseConstants.ACCOUNT_ID);
        Kv cond = Kv.by("uId",uId);
        User user = userService.get(cond,"getUserById");
        String anAuthor = user.getUName();
        announcement.setAnAuthor(anAuthor);
        announcement.setAnText(anText);
        announcement.setAnType(0);//1-内部公告
        SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date an_create = df.parse(df.format(System.currentTimeMillis()));
        announcement.setAnCreate(an_create);
        renderJson(announcement.save() ? DataResult.data(announcement) : BaseResult.fail());
    }

    /**
     * 更新外部公告
     *
     * @param anId     公告Id
     * @param anTitle  公告标题
     * @param anAuthor 公告发布者
     * @param anText   公告内容
     * @throws ParseException
     */
    @Param(name = "anId", required = true, rule = ValidateRuleConstants.Key.ID)
    @Param(name = "anTitle", required = true)
    @Param(name = "anAuthor", required = true)
    @Param(name = "anText", required = true)
    public void updateExternalAnnouncement(int anId, String anTitle, String anAuthor, String anText) throws ParseException {
        Kv cond = Kv.by("anId", anId);
        Announcement announcement = announcementService.get(cond, "getByExternalAnnouncementId");
        announcement.setAnTitle(anTitle);
        announcement.setAnAuthor(anAuthor);
        announcement.setAnText(anText);
        SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date an_update = df.parse(df.format(System.currentTimeMillis()));
        announcement.setAnUpdate(an_update);
        renderJson(announcement.update() ? DataResult.data(announcement) : BaseResult.fail());
    }


    /**
     * 更新内部公告
     *
     * @param anId     公告Id
     * @param anTitle  公告标题
     * @param anAuthor 公告发布者
     * @param anText   公告内容
     * @throws ParseException
     */
    @Param(name = "anId", required = true, rule = ValidateRuleConstants.Key.ID)
    @Param(name = "anTitle", required = true)
    @Param(name = "anAuthor", required = true)
    @Param(name = "anText", required = true)
    public void updateInternalAnnouncement(int anId, String anTitle, String anAuthor, String anText) throws ParseException {
        Kv cond = Kv.by("anId", anId);
        Announcement announcement = announcementService.get(cond, "getByInternalAnnouncementId");
        announcement.setAnTitle(anTitle);
        announcement.setAnAuthor(anAuthor);
        announcement.setAnText(anText);
        SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date an_update = df.parse(df.format(System.currentTimeMillis()));
        announcement.setAnUpdate(an_update);
        renderJson(announcement.update() ? DataResult.data(announcement) : BaseResult.fail());
    }

    /**
     * 显示最新的外部公告
     */
    @Clear(AuthInterceptor.class)
    public void showExternalAnnouncement() {
        Announcement announcement = announcementService.get("getNewestExternalAnnouncement");
        renderJson(DataResult.data(announcement));
    }

    /**
     * 显示最新的招新公告
     */
    @Clear(AuthInterceptor.class)
    public void showRecruitAnnouncement() {
        Announcement announcement = announcementService.get("getNewestRecruitAnnouncement");
        renderJson(DataResult.data(announcement));
    }

    /**
     * 查看内部公告
     */
    @Param(name = "anId", required = true, rule = ValidateRuleConstants.Key.ID)
    public void getInternalAnnouncement(int anId) {
        Kv cond = Kv.by("anId", anId);
        Announcement announcement = announcementService.get(cond, "getByInternalAnnouncementId");
        renderJson(DataResult.data(announcement));
    }


    /**
     * 分页显示内部公告
     *
     * @param page     页数
     * @param pageSize 每页的信息条数
     * @throws ParseException
     */
    @Param(name = "page", required = true)
    @Param(name = "pageSize", required = true)
    public void showInternalAnnouncementByPage(int page, int pageSize) throws ParseException {
        Page<Record> announcements = Db.paginate(page, pageSize, "select an_title,an_author,an_create,an_update", "from announcement where  an_deleted=0 and an_type=1");
        renderJson(DataResult.data(announcements));
    }


    /**
     * 分页显示外部公告
     *
     * @param page     页数
     * @param pageSize 每页的信息条数
     * @throws ParseException
     */
    @Param(name = "page", required = true)
    @Param(name = "pageSize", required = true)
    public void showExternalAnnouncementByPage(int page, int pageSize) throws ParseException {
        Page<Record> announcements = Db.paginate(page, pageSize, "select an_title,an_author,an_create,an_update", "from announcement where  an_deleted=0  and an_type=0 ");
        renderJson(DataResult.data(announcements));
    }

    /**
     * 删除公告
     *
     * @param anId 公告编号
     */
    @Param(name = "anId", required = true, rule = ValidateRuleConstants.Key.ID)
    public void deleteAnnouncement(int anId) {
        Kv cond = Kv.by("anId", anId);
        Announcement announcement = announcementService.get(cond, "getByAnnouncementId");
        announcement.setAnId(anId);
        announcement.setAnDeleted(1);
        renderJson(announcement.update() ? BaseResult.ok("删除成功") : BaseResult.fail("删除失败"));
    }

    /**
     * 管理员查看自己发布的的公告
     */
    public void getAnnouncementOfMine() {
        int uId = getAttr(BaseConstants.ACCOUNT_ID);
        Kv cond = Kv.by("uId", uId);
        List<Announcement> anlist = announcementService.list(cond, "getAnnouncementOfMine");
        renderJson(DataResult.data(anlist));
    }


}
